/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * A standard way to ask the user whether to post a bug report, which will
 * work whether the program uses Swing or not.
 */
public abstract class LogPostAsker {
    /** Prompt the user for email address and comment, asking
     *  whether to post a bug report. Returns array of email-address
     *  and comment if wants to send report, otherwise null.
     */
    public abstract boolean ask(String msg);


    private static LogPostAsker theAsker = new LogTextAsker();

    public static void setAsker(LogPostAsker asker) {
    	theAsker = asker;
    }

    public static boolean askToPost(String msg) {
    	return theAsker.ask(msg);
    }

    /** A simple, command-line BugPostAsker implementation. */
    private static class LogTextAsker extends LogPostAsker {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));

		String readLine() {
			try {
				return reader.readLine();
			} catch (IOException ex) {
				return null;
			}
		}

		public boolean ask(String msg) {
			String answer;

			System.out.println(msg);

			answer = null;
			while (answer == null
					|| (!answer.equals("y") && !answer.equals("n"))) {
				System.out.print("Post log [y/n] ? ");
				answer = readLine();
				if (answer == null) {
					System.out
							.println("Error reading stdin.  Not posting log.");
					return false;
				}
				if (answer.equals("n"))
					return false;
			}
			
			return true;
		}
	}
}


